<template>
	<view class="recharge-container page-container">
		<!-- 当前余额显示 -->
		<view class="balance-info">
			<view class="balance-card">
				<text class="balance-label">当前余额</text>
				<text class="balance-amount">¥{{ currentBalance }}</text>
			</view>
		</view>

		<!-- 充值金额输入 -->
		<view class="recharge-form">
			<view class="form-section">
				<view class="section-title">
					<text class="title-text">充值金额</text>
				</view>

				<view class="amount-input-container">
					<view class="currency-symbol">¥</view>
					<u-input v-model="rechargeAmount" type="number" placeholder="请输入充值金额" :border="false"
						:clearable="true" fontSize="32" class="amount-input" @input="onAmountInput"></u-input>
				</view>
			</view>

			<!-- 快捷金额选择 -->
			<view class="quick-amounts">
				<view class="section-title">
					<text class="title-text">快捷金额</text>
				</view>
				<view class="amount-buttons">
					<view class="amount-btn" v-for="(amount, index) in quickAmounts" :key="index"
						:class="{ active: rechargeAmount === amount.toString() }" @click="selectQuickAmount(amount)">
						<text class="amount-text">{{ amount }}</text>
					</view>
				</view>
			</view>

			<!-- 充值说明 -->
			<view class="recharge-tips">
				<view class="tips-title">
					<u-icon name="info-circle" size="16" color="#999"></u-icon>
					<text class="tips-text">充值说明</text>
				</view>
				<view class="tips-content">
					<text class="tip-item">• 充值金额最低10元，最高单次充值不超过10000元</text>
					<text class="tip-item">• 充值成功后，余额将立即到账</text>
					<text class="tip-item">• 如有疑问，请联系客服</text>
				</view>
			</view>
		</view>

		<!-- 底部充值按钮 -->
		<view class="bottom-action">
			<button class="recharge-btn" :class="{ disabled: !canRecharge }" :disabled="!canRecharge"
				@click="handleRecharge">
				立即充值
			</button>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'

	export default {
		data() {
			return {
				// 当前余额
				currentBalance: '0.00',
				// 充值金额
				rechargeAmount: '',
				// 快捷金额选项
				quickAmounts: [50, 100, 200, 500, 1000, 2000, 0.01]
			}
		},
		computed: {
			...mapGetters(['isLogin', 'openid', 'userInfo']),
			// 是否可以充值
			canRecharge() {
				const amount = parseFloat(this.rechargeAmount)
				// 正式发布需要注释掉
				// return amount >= 10 && amount <= 10000
				return amount > 0 && amount <= 10000
			}
		},
		onShow() {
			this.loadAccountInfo();
		},
		methods: {
			// 查询账户信息
			loadAccountInfo() {
				this.$api.user.GetAccountInfo().then(res => {
					if (res.data && res.data.balance) {
						this.currentBalance = res.data.balance
					}
				})
			},

			// 金额输入处理
			onAmountInput(value) {
				// 限制小数点后两位
				if (value.includes('.')) {
					const parts = value.split('.')
					if (parts[1] && parts[1].length > 2) {
						this.rechargeAmount = parts[0] + '.' + parts[1].substring(0, 2)
					}
				}
			},

			// 选择快捷金额
			selectQuickAmount(amount) {
				this.rechargeAmount = amount.toString()
			},

			// 处理充值
			handleRecharge() {
				if (!this.canRecharge) {
					return
				}

				const amount = parseFloat(this.rechargeAmount)

				// 显示确认弹窗
				uni.showModal({
					title: '确认充值',
					content: `确认充值 ¥${amount} 到账户余额？`,
					success: (res) => {
						if (res.confirm) {
							this.$api.user.AddRechargeOrders({
								open_user_id: this.openid,
								recharge_amount: amount,
								pay_amount: amount,
								pay_type: 1,
								pay_type_desc: '微信支付',
								remark: ''
							}).then(res => {
								this.processRecharge(res.data)
							})
						}
					}
				})
			},

			// 处理充值流程
			processRecharge(orderid) {
				// 显示加载提示
				uni.showLoading({
					title: '处理中...'
				})

				this.$api.user.PayRechargeOrders({
					openid: this.openid,
					id: orderid,
					good_desc: '充值'
				}).then(res => {
					uni.hideLoading();
					// 发起微信支付
					uni.requestPayment({
						timeStamp: res.data.timeStamp,
						nonceStr: res.data.nonceStr,
						package: res.data.package,
						signType: res.data.signType,
						paySign: res.data.paySign,
						success: res => {
							uni.showToast({
								title: res.msg,
								icon: 'success',
								duration: 2000
							})

							setTimeout(() => {
								uni.redirectTo({
									url: `/pages/payment/success?amount=${this.rechargeAmount}`
								})
							}, 1500)
						},
						fail: err => {
							// console.log('订单支付失败：' + err)
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.recharge-container {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
	}

	.balance-info {
		margin-bottom: 30rpx;
	}

	.balance-card {
		background: linear-gradient(135deg, #52a58a 0%, #459a7e 100%);
		border-radius: 16rpx;
		padding: 30rpx;
		text-align: center;
		color: white;
	}

	.balance-label {
		font-size: 26rpx;
		opacity: 0.9;
		display: block;
		margin-bottom: 10rpx;
	}

	.balance-amount {
		font-size: 48rpx;
		font-weight: 600;
	}

	.recharge-form {
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
	}

	.form-section {
		margin-bottom: 40rpx;
	}

	.section-title {
		margin-bottom: 20rpx;
	}

	.title-text {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}

	.amount-input-container {
		display: flex;
		align-items: center;
		background-color: #f8f9fa;
		border-radius: 12rpx;
		padding: 20rpx;
		border: 2rpx solid #e5e5e5;
	}

	.currency-symbol {
		font-size: 32rpx;
		color: #333;
		margin-right: 10rpx;
		font-weight: 600;
	}

	.amount-input {
		flex: 1;
	}

	.quick-amounts {
		margin-bottom: 40rpx;
	}

	.amount-buttons {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
	}

	.amount-btn {
		flex: 1;
		min-width: 140rpx;
		padding: 20rpx;
		background-color: #f8f9fa;
		border: 2rpx solid #e5e5e5;
		border-radius: 12rpx;
		text-align: center;
		transition: all 0.3s ease;
	}

	.amount-btn.active {
		background-color: #52a58a;
		border-color: #52a58a;
	}

	.amount-btn.active .amount-text {
		color: white;
	}

	.amount-text {
		font-size: 28rpx;
		color: #333;
		font-weight: 500;
	}

	.recharge-tips {
		background-color: #f8f9fa;
		border-radius: 12rpx;
		padding: 20rpx;
	}

	.tips-title {
		display: flex;
		align-items: center;
		gap: 8rpx;
		margin-bottom: 15rpx;
	}

	.tips-text {
		font-size: 26rpx;
		color: #666;
		font-weight: 500;
	}

	.tips-content {
		padding-left: 24rpx;
	}

	.tip-item {
		display: block;
		font-size: 24rpx;
		color: #999;
		line-height: 1.6;
		margin-bottom: 8rpx;
	}

	.bottom-action {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		background-color: #ffffff;
		border-top: 1rpx solid #e5e5e5;
	}

	.recharge-btn {
		width: 100%;
		height: 88rpx;
		background-color: #52a58a;
		color: white;
		border: none;
		border-radius: 12rpx;
		font-size: 32rpx;
		font-weight: 600;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.recharge-btn.disabled {
		background-color: #cccccc;
		color: #999999;
	}
</style>